﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

namespace DEMI_webapp
{
    public partial class Edycja_kapeluszy : System.Web.UI.Page
    {
        System.Configuration.ConnectionStringSettings connString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["conString"];

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button_Usun_Click(object sender, ImageClickEventArgs e)
        {
            GridViewRow row = GridView1.SelectedRow;
            int ilosc = Convert.ToInt32(row.Cells[5].Text);

            if (ilosc != 0)
            {
                L_uwaga.Text = "Nie można usunąć kapelusza którego ilość jest większa od 0 !!!";
            }
            else
            {
                Panel1.Visible = true;
                L_uwaga.Text = "";
            }
        }


        protected void Button_Ilosc_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                Convert.ToInt32(TB_Ilosc.Text);
            }
            catch
            {
                L_uwaga.Text = "Błedne dane w polu Ilość !!!";
                return;
            }

            if (TB_Ilosc.Text != "")
            {
                SqlConnection con;
                con = new SqlConnection(connString.ConnectionString);
                con.Open();

                GridViewRow row = GridView1.SelectedRow;

                SqlCommand edycja = new SqlCommand("UPDATE Kapelusze SET ilosc=@Ilosc WHERE nazwa=@Nazwa and kolor=@Kolor", con);

                SqlParameter param = new SqlParameter();
                param.ParameterName = "@Nazwa";
                param.SqlDbType = SqlDbType.VarChar;
                param.Value = row.Cells[1].Text;
                edycja.Parameters.Add(param);

                SqlParameter param1 = new SqlParameter();
                param1.ParameterName = "@Kolor";
                param1.SqlDbType = SqlDbType.VarChar;
                param1.Value = row.Cells[2].Text;
                edycja.Parameters.Add(param1);

                SqlParameter param2 = new SqlParameter();
                param2.ParameterName = "@Ilosc";
                param2.SqlDbType = SqlDbType.Int;
                param2.Value = Convert.ToInt32(TB_Ilosc.Text);
                edycja.Parameters.Add(param2);

                edycja.ExecuteNonQuery();
                L_uwaga.Text = "";
                con.Close();
                GridView1.DataBind();
            }
            else
                L_uwaga.Text = "Podaj ilość !!!";
        }

        protected void Button_Zatwierdz_Click(object sender, ImageClickEventArgs e)
        {
			if (RadioButtonList1.Items.FindByText("Tak").Selected == true)
			{
				GridViewRow row = GridView1.SelectedRow;
				SqlConnection con;
				con = new SqlConnection(connString.ConnectionString);
				con.Open();

				SqlCommand usun = new SqlCommand("DELETE FROM Kapelusze WHERE nazwa=@Nazwa and kolor=@Kolor", con);

				SqlParameter param = new SqlParameter();
				param.ParameterName = "@Nazwa";
				param.SqlDbType = SqlDbType.VarChar;
				param.Value = row.Cells[1].Text;
				usun.Parameters.Add(param);

				SqlParameter param1 = new SqlParameter();
				param1.ParameterName = "@Kolor";
				param1.SqlDbType = SqlDbType.VarChar;
				param1.Value = row.Cells[2].Text;
				usun.Parameters.Add(param1);

				try
				{
					usun.ExecuteNonQuery();
				}
				catch (SqlException)
				{
					Response.Write("<script>alert('Nie można usunąć kapelusza gdyż istnieją do niego odnośniki w historii magazynowej.')</script>");
					Panel1.Visible = false;
					L_uwaga.Text = "";
					con.Close();
					return;
				}
				L_uwaga.Text = "";
				con.Close();
				GridView1.DataBind();
				Panel1.Visible = false;
			}
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Panel1.Visible = false;
        }
    }
}